/*--------------------------------------------------------------
 * File: demo38.c 编码类型：C语言  UTF-8
 * Author WRS (1352509846@qq.com)
 * 功能: 二进制与十进制互相转换
 * Version 0.1
 * Date 2022-03-07 
 * 
 * @copyright Copyright (c) 2022
 * 
----------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>

// 二进制转换成十进制
int converBinaryToDecimal(long long n)
{
    int decimalNumber = 0;
    int i = 0;
    int remainder;

    while(n != 0)
    {
        remainder = n%10;
        n /= 10;
        decimalNumber += remainder * pow(2, i);
        ++i;
    }
    return decimalNumber;
}

// 十进制转换成二进制
long long converDecimalToBinary(int n)
{
    long long BinaryNumber = 0;
    int i = 1;
    int remainder;
    int step = 1;
    
    while(n != 0)
    {
        remainder = n%2;
        printf("Step %d: %d/2, 余数 = %d, 商 = %d\n",step++, n, remainder, n/2);
        n /= 2;
        BinaryNumber += remainder*i;
        i *= 10;
    }
    return BinaryNumber;
}

int main(int argc, char const *argv[])
{
    int n;
    printf("请输入一个十进制数：");
    scanf("%d",&n);
    printf("十进制数%d转换成二进制是%lld\n",n,converDecimalToBinary(n));
    return 0;
}
